mui.init();
showLoading();
var max = document.documentElement.clientWidth;
var Wcitylist; //城市列表页
var adUrl = getUrl("75getAdvert"); //广告url
var nearBy = getUrl("85nearby");
var ocityNoCircle = document.getElementById("bg");
var mycircle = document.getElementById("mycircle");
var currentCity = readData(localStorage.currentCity);
var lat = readData(localStorage.currentLat);
var lon = readData(localStorage.currentLon)
var win = getViewWH();

//vue实例化对象
var home = new Vue({
		el: "#home",
		methods: {
			loadMore: loadMore,
			opencity: opencity,
			openGuanzhu: openGuanzhu,
			openCircle: openCircle,
			openProduct: detail,
			openMessage: openMessage,
			openMarket: openMarket,
			nearByMarket: nearByMarket
		},
		data: {
			Circles: [], //未关注圈子
			myCircles: [],
			markets: [],
			marketPicSize: {}
		},
		ready: function() {
			//请求广告
			this.$http({
				url: adUrl,
				method: 'post',
				params: {
					advertLevel: "system",
				}
			}).then(function(repson) {
				var data = repson.data;
				if (data.lunList.tops[0]) {
					var toplunList = data.lunList.tops[0].images.split(',');
					this.$set('sliderPic', toplunList);
				}
				if (data.textList.tops) {
					this.$set('toptextlist', data.textList.tops)
				}
				if (data.picList.mids[0]) {
					var picAd = data.picList.mids[0].images;
					this.$set('picAd', picAd);
				}
				setTimeout(function() {
					initSwiper();
				}, 100)
				removeLoading()
			}, function(response) {
				removeLoading()
			});

			//取得登陆后关注的圈子
			if (localStorage.account) {
				//hasUread(readData(localStorage.noticeToken));//未读消息提示
				
				var circleurl = getUrl("86myAttention"); //关注圈子
				var headerJson = getHeader();
				var cid = readData(localStorage.account);
				this.$http({
					url: circleurl,
					method: 'post',
					headers: headerJson,
					data: "accountId=" + cid
				}).then(function(repson) {
						var data = repson.data;
						if (data.code == "0") {
							mycircle.style.display = "block";
							var useIds = [];
							if (data.obj) {
								for (var i in data.obj) {
									useIds.push(data.obj[i].userId);
								}
							}
							loginRong(useIds); //融云登录
							var circle = data.obj;
							this.$set('myCircles', circle)
							guanzhucid = data.obj;
							if (data.obj.length <= 0) {
								$scope.imghide = true;
							}

						} else {
							console.log("网络链接异常，请稍后重试" + data.code)
						}
					},
					function(response) {});
			}
		}
	})
	//下拉加载未关注圈子
var ipagenum = 0;
var ipagesizenum = 8;
var serverNum = 0;
var Olocation = document.getElementById("location")
	//初始化推荐圈子加载下拉
function initLoadMore() {
	ipagenum = 0;
	ipagesizenum = 8;
	serverNum = 0;
	home.Circles = [];
	bRefresh = true;
	home.nearByMarket();
	//默认加载未关注圈子
	home.loadMore();

}
//附近商场
function nearByMarket() {
	home.$http({
		url: nearBy,
		method: 'post',
		params: {
			cityName: readData(localStorage.currentCity),
			longitude: lon,
			latitude: lat
		}
	}).then(function(repson) {
		//alert("广告：" + JSON.stringify(repson));
		data = repson.data.obj.nearbyShops;
		for (var i = 0; i < data.length; i++) {
			data[i].distance = (data[i].distance / 1000).toFixed(0)
		}
		var win = getViewWH();
		home.marketPicSize = {
			picW: win.winW + "px",
			picH: 200 * (win.winW / 500) + "px"
		};
		home.markets = data;
	}, function(response) {

	});
}

//未关注圈子请求
function loadMore() {
	var params1, request, noCircle = getUrl("93recommendCircle");
	Olocation.innerText = readData(localStorage.currentCity);
	if (localStorage.account) {
		params1 = "indexPage=" + ipagenum + "&pageSize=" + ipagesizenum + "&city=" + readData(localStorage.currentCity) + "&accountId=" + readData(localStorage.account);
		request = {
			url: noCircle,
			method: 'post',
			headers: getHeader(),
			data: params1
		}
	} else {
		params1 = {
			"indexPage": ipagenum,
			"pageSize": ipagesizenum,
			"city": readData(localStorage.currentCity)
		}
		request = {
			url: noCircle,
			method: 'post',
			params: params1
		}
	}
	home.$http(request).then(function(repson) {
		//alert("推荐圈子数据：" + JSON.stringify(repson));
		var data = repson.data;
		ipagenum++;
		bRefresh = true;
		if (data.obj) {
			var circle = data.obj.list;
			serverNum = data.obj.totalPage;
			for (var i = 0; i < circle.length; i++) {
				if (!circle[i].shopLogo) {
					circle[i].shopLogo = "../image/small.png";
				}
				home.Circles.push(circle[i]);
			}
			mui(".mui-pull-bottom-tips")[0].style.display = "block";
			ocityNoCircle.style.display = "none";
		} else {
			mui(".mui-pull-bottom-tips")[0].style.display = "none";
			if (serverNum == 0) {
				ocityNoCircle.style.display = "block";
			} else {
				mui.toast("没有数据了")
			}
		}

	}, function(response) {
		bRefresh = true;
	});
}
initLoadMore();
//轮播图初始化
function initSwiper() {
	var mySwiper = new Swiper('.swiper-container', {
		direction: 'horizontal',
		loop: true,
		autoplay: 5000,
		speed: 1000,
		pagination: '.swiper-pagination'
	});
	new Slide("outwrapper", "inslide", 3000).init();
}
var bRefresh = true;
//监听事件 城市列表页面出发changecity事件
window.addEventListener("changeCity", function() {
		initLoadMore();
	})
	//预加载城市列表页
document.addEventListener("plusready", function() {
	Wcitylist = mui.preload({
		url: "cityList.html",
		id: "cityList.html"
	});
	Wcitylist.hide();
	pullLoad();
}, false);

function pullLoad() {
	//下拉加载
	mui(".mui-scroll").pullToRefresh({
		up: {
			contentrefresh: "努力加载…",
			contentnomore: "没有更多数据了",
			callback: function() {
				var self = this;
				if (ipagenum <= serverNum) {
					if (bRefresh) {
						bRefresh = false;
						home.loadMore();
					}
				}
				setTimeout(function() {
					self.endPullUpToRefresh(false);
				}, 1000)
			}
		}
	});
}
//根据滚动事件控制头部变化
window.onscroll = myFunction;

function myFunction() {
	//console.log(document.body.scrollTop);
	var topscroll = document.body.scrollTop;
	var header = mui("#header")[0];
	var backtop = document.getElementById("to_top");
	var msg = document.getElementById('info');
	if (topscroll > 50) {
		msg.style.borderColor = '#fff';
		header.style.background = "#da4453";
		header.style.color = "#fff";
		if (topscroll > 400) {
			backtop.style.display = "block";
		}
	} else {
		msg.style.borderColor = 'red';
		header.style.background = "rgba(255, 255, 255, .5)";
		header.style.color = "#000";
		backtop.style.display = "none";
	}
}
//打开城市列表页
function opencity() {
	Wcitylist.show("slide-in-bottom");
}

function openMessage() {
	mui.openWindow({
		url: 'my/message.html',
		id: 'message.html',
		waiting: {
			autoShow: false
		}
	})
}

function openGuanzhu(uid, groupId) {
	var e = event;
	e.stopPropagation()
	localStorage.guideId = writeData(uid);
	OpenNewWin({
		url: "shopping/guanzhu.html",
		id: 'guanzhu.html'
	})
}
//打开圈子进入
function openCircle(gid, cname, groupId, cId) {
	localStorage.guideId = writeData(gid);
	localStorage.circleName = writeData(cname);
	localStorage.groupId = writeData(groupId);
	localStorage.circleId = writeData(cId);
	OpenNewWin({
		url: "shopping/circleMain.html",
		id: "circleMain.html"
	})
}
//打开商品详情
function detail(id, gid, cname, groupId) {
	//获取商品ID，打开商品详情页
	localStorage.goodsId = writeData(id);
	localStorage.guideId = writeData(gid);
	localStorage.circleName = writeData(cname);
	localStorage.groupId = writeData(groupId);
	OpenNewWin({
		url: "shopping/productDetail.html",
		id: "productDetail.html"
	})
}

function openMarket(makId) {
	localStorage.marketId = writeData(makId);
	console.log(makId);
	OpenNewWin({
		url: "shopping/market.html",
		id: 'market.html'
	})
}